package org.lanqiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lanqiao.collect.ICollectService;
import org.lanqiao.information.IInfoService;
import org.lanqiao.information.Information;
import org.lanqiao.mapper.InfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class InfoServiceImpl implements IInfoService {
    @Autowired
    private InfoMapper informationMapper;
    @Autowired
    private ICollectService iCollectService;
    @Override
    //实现根据创建时间的排序（倒序），需要传入两个分类id(可选择性的传入)
    public List<Information> InforListByCreateTime(Integer sort1id, Integer sort2id) {
        if (sort2id == null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",1)
                    .orderByDesc("create_time")
            );
        } else  {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",1)
                    .orderByDesc("create_time")
            );
        }
        }

    @Override
    //根据阅读量排序（倒序）
    public List<Information> InforListByReads(Integer sort1id, Integer sort2id) {
        if (sort2id == null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",1)
                    .orderByDesc("reads_num")
            );
        }else {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",1)
                    .orderByDesc("reads_num")
            );
        }
    }

    @Override
    //点赞排序（倒序）
    public List<Information> InforListByLike(Integer sort1id, Integer sort2id) {
        if (sort2id == null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",1)
                    .orderByDesc("like_count")
            );
        }else {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",1)
                    .orderByDesc("like_count")
            );
        }
    }

    @Override
    //按照收藏量排序（倒序）
    public List<Information> InforListByCollect(Integer sort1id, Integer sort2id) {
        if (sort2id == null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",1)
                    .orderByDesc("collect_num")
            );
        }else {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",1)
                    .orderByDesc("collect_num")
            );
        }
    }
    @Override
    //所有待审核的信息
    public List<Information> InfoListByChecking(Integer sort1id, Integer sort2id) {
        if(sort1id == null && sort2id == null){
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("info_status",0)
                    .orderByDesc("create_time")
            );
        }
       else if (sort1id != null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",0)
                    .orderByDesc("create_time")
            );
        }else {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",0)
                    .orderByDesc("create_time")
            );
        }
    }

    @Override
    //所有待被举报的信息
    public List<Information> InfoListByReport(Integer sort1id, Integer sort2id) {
        if(sort1id == null && sort2id == null){
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("info_status",3)
                    .orderByDesc("create_time")
            );
        }
        else if (sort1id != null) {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("info_status",3)
                    .orderByDesc("create_time")
            );
        }else {
            return informationMapper.selectList(new QueryWrapper<Information>()
                    .eq("sort1_id", sort1id)
                    .eq("sort2_id", sort2id)
                    .eq("info_status",3)
                    .orderByDesc("create_time")
            );
        }
    }

    @Override
    //条件查询
    public  List<Information> QueryByCondition(Map<String, Object> queryMap) {
       return informationMapper.selectList(queryWrapper1(queryMap));
    }
    //查询条件
    private QueryWrapper<Information> queryWrapper1(Map<String,Object> queryMap){
        QueryWrapper<Information> queryWrapper = null;
        if(queryMap !=null){
            queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(queryMap.get("infotitle"))){
                queryWrapper.like("info_title",queryMap.get("infotitle"));
            }
           if(!StringUtils.isEmpty(queryMap.get("infocontent"))){
                queryWrapper.like("info_content",queryMap.get("infocontent"));
            }
            if(!StringUtils.isEmpty(queryMap.get("createtime"))){
                //大于等于创建时间 create_time》=所给时间
               queryWrapper.ge("create_time",queryMap.get("createtime"));
            }
            if(!StringUtils.isEmpty(queryMap.get("updatetime"))){
                //小于等于更新时间  update_time《=所给时间
                queryWrapper.le("update_time",queryMap.get("updatetime"));
            }
            if(!StringUtils.isEmpty(queryMap.get("sort1id"))){
                queryWrapper.eq("sort1_id", queryMap.get("sort1id"));
            }
            if(!StringUtils.isEmpty(queryMap.get("sort2id"))){
                queryWrapper.eq("sort2_id", queryMap.get("sort2id"));
            }
            if(!StringUtils.isEmpty(queryMap.get("userid"))){
                queryWrapper.eq("user_id", queryMap.get("userid"));
            }
            if(!StringUtils.isEmpty(queryMap.get("infourl"))){
                queryWrapper.like("info_url", queryMap.get("infourl"));
            }
            if(!StringUtils.isEmpty(queryMap.get("infostatus"))){
                queryWrapper.like("info_status", queryMap.get("infostatus"));
            }
            if(!StringUtils.isEmpty(queryMap.get("orderByAsc"))){
                queryWrapper.orderByAsc((String) queryMap.get("orderByAsc"));
            }
            if(!StringUtils.isEmpty(queryMap.get("orderByDesc"))){
                queryWrapper.orderByDesc((String) queryMap.get("orderByDesc"));
            }
        }
        return  queryWrapper;
    }

    @Override
    public IPage<Information> InfoListPage(Integer sort1id, Integer sort2id, Integer pagenum, Integer pagesize) {
        IPage<Information> page = new Page<>(pagenum,pagesize);
        page = informationMapper.selectPage(page,new QueryWrapper<Information>()
                .eq("sort1_id", sort1id)
                .eq("sort2_id", sort2id)
                .eq("info_status",1)
        );
        return page;
    }
    @Override
    //分页条件查询
    public IPage<Information> QueryConditionPage(Map<String, Object> queryMap, Integer pagenum, Integer pagesize) {
        IPage<Information> page = new Page<>(pagenum,pagesize,true);
        page = informationMapper.selectPage(page,queryWrapper1(queryMap));
        return page;
    }

    @Override
    //新增资源
    public Integer addInformation(Integer userid, Integer sort1id, Integer sort2id, String infotitle, String infocontent ,String infourl) {
        Date creatdate =new Date();
        Information information =new Information(creatdate,sort1id,sort2id,infotitle,infocontent,userid,infourl);
        return informationMapper.insert(information);
    }

    @Override
    //更新资源 可以更新的数据有sort1id sort2id infotitle infocontent
    public Integer updateInformation(Integer id, Integer sort1id, Integer sort2id, String infotitle, String infocontent, String infourl) {
        Date updatedate =new Date();
        Information information =new Information(id,updatedate,sort1id,sort2id,infotitle,infocontent,infourl);
        return informationMapper.updateById(information);
    }

    @Override
    //删除资源
    public Integer deleteInfomation(Integer id) {
       iCollectService.deletelist(id); //在删除资源的同时删除与资源相关的所有收藏贴
       return informationMapper.deleteById(id);
    }

    @Override
    //根据id查询某一条信息
    public Information selectOnes(Integer id) {
        return informationMapper.selectOne(new QueryWrapper<Information>()
                .eq("info_id",id)
        );
    }

    @Override
    //获取点赞数
    public Integer getLikeConunt(Integer id) {
        return informationMapper.selectOne(new QueryWrapper<Information>()
                .eq("info_id",id)
                .eq("info_status",1)
        ).getLikecount();
    }

    @Override
    //获取回复数
    public Integer getResponse(Integer id) {
        return informationMapper.selectOne(new QueryWrapper<Information>()
                .eq("info_id",id)
        ).getInforesponse();
    }

    @Override
    //获取访问数
    public Integer getReads(Integer id) {
        return informationMapper.selectOne(new QueryWrapper<Information>()
                .eq("info_id",id)
                .eq("info_status",1)
        ).getReadsnum();
    }

    @Override
    //点赞数加一
    public Integer addoneLikeCount(Integer id) {
        Information information =selectOnes(id);
        information.setLikecount(information.getLikecount()+1);
        return informationMapper.updateById(information);
    }

    @Override
    //点赞数减一
    public Integer deloneLikeCount(Integer id) {
        Information information =selectOnes(id);
        if(information.getLikecount()>0){
            information.setLikecount(information.getLikecount()-1);
        }
        return informationMapper.updateById(information);
    }

    @Override
    //回复数加一
    public Integer addoneResponse(Integer id) {
        Information information =selectOnes(id);
        information.setInforesponse(information.getInforesponse()+1);
        return informationMapper.updateById(information);
    }

    @Override
    //回复数减一
    public Integer deloneResponse(Integer id) {
        Information information =selectOnes(id);
        if(information.getInforesponse()>0){
            information.setInforesponse(information.getInforesponse()-1);
        }
        return informationMapper.updateById(information);
    }

    @Override
    //访问数加一
    public Integer addoneReads(Integer id) {
        Information information =selectOnes(id);
        information.setReadsnum(information.getReadsnum()+1);
        return informationMapper.updateById(information);
    }

    @Override
    //改变状态
    public Integer ChangeStatus(Integer id, Integer status) {
        Information information =selectOnes(id);
        information.setInfostatus(status);
        return informationMapper.updateById(information);
    }

    @Override
    //收藏数加一
    public Integer addoneCollectNum(Integer id) {
        Information information =selectOnes(id);
        information.setColllectnum(information.getColllectnum()+1);
        return informationMapper.updateById(information);
    }

    @Override
    //收藏数减一
    public Integer DeloneCollectNum(Integer id) {
        Information information =selectOnes(id);
        if(information.getColllectnum()>0){
            information.setColllectnum(information.getColllectnum()-1);
        }
        return informationMapper.updateById(information);
    }

    @Override
    //举报资源
    public Integer InfoReport(Integer infoid, Integer tipuserid, String tipcontent) {
        Information information =selectOnes(infoid);
        information.setTipuserid(tipuserid);
        information.setTipcontent(tipcontent);
        information.setInfostatus(3);
        return informationMapper.updateById(information);
    }

    @Override
    //举报成功
    public Integer ReportSuccess(Integer id) {
        return ChangeStatus(id,4);
    }

    @Override
    //举报失败 将举报信息消除 并将状态改回1
    public Integer ReportFail(Integer id) {
        Information information =selectOnes(id);
        information.setTipuserid(null);
        information.setTipcontent(null);
        information.setInfostatus(1);
        System.out.println(information.toString());
        return informationMapper.updateById(information);
    }

}
